import { ref, reactive, onMounted } from "vue";
import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
import {
  ShowTreeNodeInfo,
  GetAllByPagedAndSort,
  EnterGetAllByPagedAndSort,
  GetEnterpriseModelNameOptions,
  CalculateEnterpriseCredit,
  EnterpriseCreditResultDGaeeilstt,
  EnterpriseCreditScoreMeasurementAGellt,
  EnterpriseCreditResultGetAllByPagedAndSort,
  EnterpriseCreditModelGetAllByPagedAndSort
} from "@/api/stu/DataScene";
import qs from "qs";

export function EnterpriseMarketCredit() {
  // 信用数据
  const route = useRoute();
  const dataList = ref<any>();
  const loading = ref<boolean>(true);
  const keepmodel = ref(null);
  const ScoreParticulars_ = ref();
  const tabsmodel = ref('1');
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "企业姓名",
      prop: "enterpriseName",
      minWidth: 150
    }, {
      label: "注册地址",
      prop: "registerAddress",
      minWidth: 100
    },
    {
      label: "登记注册类型",
      prop: "registerType",
      minWidth: 110,
      cellRenderer: ({ row }) => (
        <el-tag type={
          row.registerType ? "warning" : "primary"}>
          {
            row.registerType ? "外商投资企业" : "内资企业"
          }
        </el-tag>
      )
    },
    {
      label: "登记注册号",
      prop: "registerNo",
      minWidth: 100
    },
    {
      label: "登记注册日期",
      prop: "registerDate",
      minWidth: 200
    },
    {
      label: "有效截至日期",
      prop: "endDate",
      minWidth: 150
    },
    {
      label: "组织机构代码",
      prop: "organizationCode",
      minWidth: 150
    },
    {
      label: "中证码",
      prop: "strikeCode",
      minWidth: 100
    },
    {
      label: "国税登记号",
      prop: "nationalTaxNo",
      minWidth: 200
    },
    {
      label: "地税登记号",
      prop: "landTaxNo",
      minWidth: 200
    },
    {
      label: "证件号码",
      prop: "certificateNo",
      minWidth: 200
    },
    {
      label: "法定代表人",
      prop: "corporateName",
      minWidth: 200
    },
  ]
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });

  // 评分模型列表
  const ModeDetails_ = ref();
  const REFModeDetails_ = ref();
  const dataList2 = ref<any>();
  const loading2 = ref<boolean>(true);
  const columns2: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "模型名称",
      prop: "modelName",
      minWidth: 150
    },
    // {
    //   label: "创建时间",
    //   prop: "createTime",
    //   minWidth: 150
    // },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ]
  // 分页
  const pagination2 = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns3: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "企业姓名",
      prop: "enterpriseName",
      minWidth: 150
    },
    {
      label: "企业法定代表人姓名",
      prop: "enterpriseLegaler",
      minWidth: 200
    },
    {
      label: "企业法人身份证号码",
      prop: "certificateNo",
      minWidth: 150
    },
    {
      label: "评分模型",
      prop: "modelName",
      minWidth: 100
    },
    {
      label: "信用分",
      prop: "creditScore",
      minWidth: 200
    },
    {
      label: "信用等级",
      prop: "creditRating",
      minWidth: 200
    }, {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ]
  // 分页
  const pagination3 = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });



  // 信用分测算
  // 测算数据 列表
  const ENdodelNameList = ref([]) // 模型名称
  const ENApplynterpriseList = ref([])//申请测算企业名称
  // 测算数据 -- 选中的数据
  const EnterpriseCalculateFrom = reactive<any>({
    dodelName: "", // 模型名称
    Applynterprise: ""//申请测算企业名称
  })
  // 测算数据 end
  const dataList3 = ref<any>();
  const loading3 = ref<boolean>(false);
  // 查看详情
  async function ShowDetails(row) {
    const data = qs.stringify({ enterpriseCreditModelId: row.enterpriseCreditModelId });
    const res = await ShowTreeNodeInfo(data);
    if (res.success) {
      ModeDetails_.value = {
        data: res.data,
        type: "2"
      };
      // GetModelList()
      REFModeDetails_.value.ModeVisible = true;
    } else {
      ElMessage.error(res.msg)
    }
  }

  // 新建评分模型
  async function NewScoringModel() {
    keepmodel.value.SetModeVisible();
  }

  // 查看详情
  async function ShowDetails3(row) {
    const data = qs.stringify({
      modelId: row.modelId,
      enterpriseId: row.enterpriseId
    });
    const res = await EnterpriseCreditResultDGaeeilstt(data);
    if (res.success) {
      const data = {
        data: res.data,
        grade: row.creditRating,
        score: row.creditScore
      }
      ScoreParticulars_.value.ViewScore(data);
    } else {
      ElMessage.error(res.msg)
    }

  }


  //////////

  // 测算
  async function ChangeCalculate() {
    if (EnterpriseCalculateFrom.Applynterprise == "" || EnterpriseCalculateFrom.dodelName == "") {
      ElMessage.warning("请输入企业名称和模型名称");
      return;
    }
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      EnterpriseId: EnterpriseCalculateFrom.Applynterprise.enterpriseId,
      EnterpriseName: EnterpriseCalculateFrom.Applynterprise.enterpriseName,
      EnterpriseCreditModelId: EnterpriseCalculateFrom.dodelName.enterpriseCreditModelId,
      EnterpriseCreditModelName: EnterpriseCalculateFrom.dodelName.modelName
    }
    const res = await CalculateEnterpriseCredit(data);
    if (res.success) {
      // pagination3.total = res.data.totalCount;
      // dataList3.value = res.data.items;
      GetModelList()
      loading3.value = false;
      ElMessage.success(res.msg);
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading3.value = false }, 1500)
    }
  }

  function handleSizeChange(val) {
    GetMarketCreditData()
  }

  function handleCurrentChange(val) {
    GetMarketCreditData()
  }



  function handleSizeChange2(val) {
    GetModelGradeList()
  }

  function handleCurrentChange2(val) {
    GetModelGradeList()
  }

  function handleSizeChange3(val) {
    GetModelList()
  }

  function handleCurrentChange3(val) {
    GetModelList()
  }

  // 切换表单时请求数据
  function HandTabClick({ props }) {
    // console.log("tabls", props)
    if (props.name == "2") {
      GetModelGradeList()
    }
    if (props.name == "3") {
      GetOptions2()
      GetModelList()
    }
  }



  // 获取企業市场化信用数据
  async function GetMarketCreditData() {
    const res = await EnterGetAllByPagedAndSort({
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      planId: route.query.planId,
      taskId: route.query.taskId,
    });

    if (res.success) {
      pagination.total = res.data.totalCount;
      dataList.value = res.data.items;
      loading.value = false
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false }, 1500)
    }
  }

  // 获取测算模型结果列表

  async function GetModelList() {
    const data = {
      pageIndex: pagination3.currentPage,
      pageCount: pagination3.pageSize,
      planId: route.query.planId,
      taskId: route.query.taskId,
    };
    const res = await GetAllByPagedAndSort(data);
    if (res.success) {
      loading3.value = false;
      dataList3.value = res.data.items;
      pagination3.total = res.data.totalCount
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading3.value = false }, 1500)
    }
  }

  async function GetOptions() {
    const params = qs.stringify({
      taskId: route.query.taskId,
      planId: route.query.planId
    })
    // 获取企业名称
    const res2 = await EnterpriseCreditScoreMeasurementAGellt(params);
    if (res2.success) {
      ENApplynterpriseList.value = res2.data; // TOD
    } else {
      ElMessage.error(res2.msg);
    }
  }

  async function GetOptions2() {
    const data = qs.stringify({
      taskId: route.query.taskId,
      planId: route.query.planId
    })
    const res3 = await GetEnterpriseModelNameOptions(data);
    if (res3.success) {
      // loading2.value = false;
      // dataList2.value = res3.data.items;
      ENdodelNameList.value = res3.data.items;
      // pagination2.total = res3.data.totalCount
    } else {
      ElMessage.error(res3.msg);
      // setTimeout(() => { loading3.value = false }, 1500)
    }
  }

  async function GetModelGradeList() {
    const data = {
      pageIndex: pagination2.currentPage,
      pageCount: pagination2.pageSize,
      planId: route.query.planId,
      taskId: route.query.taskId,
    };
    const res = await EnterpriseCreditModelGetAllByPagedAndSort(data);
    if (res.success) {
      loading2.value = false;
      dataList2.value = res.data.items;
      pagination2.total = res.data.totalCount
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading2.value = false }, 1500)
    }
  }


  onMounted(() => {
    GetMarketCreditData();
    GetOptions()
    GetOptions2()
    GetModelList()
    GetModelGradeList()
  })
  return {
    dataList,
    loading,
    columns,
    tabsmodel,
    pagination,
    dataList2,
    loading2,
    columns2,
    pagination2,
    dataList3,
    loading3,
    columns3,
    keepmodel,
    pagination3,
    ModeDetails_,
    REFModeDetails_,
    ENdodelNameList,
    ScoreParticulars_,
    ENApplynterpriseList,
    EnterpriseCalculateFrom,
    HandTabClick,
    GetOptions2,
    GetModelGradeList,
    ChangeCalculate,
    ShowDetails,
    ShowDetails3,
    NewScoringModel,
    handleSizeChange,
    handleCurrentChange,
    handleSizeChange2,
    handleCurrentChange2,
    handleSizeChange3,
    handleCurrentChange3,
    GetMarketCreditData
  }
}
